﻿/// ZaxControls
/// Programmed by Leland Ede
/// Website: http://www.zaxisstudios.com
/// E-Mail: codeplex@zaxis-studios.com
/// Create: 4/02/2014
/// Modified: 4/09/2014

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;

using ZaxLib.Web.Writers;
using ZaxLib.Web.Writers.Nodes;

namespace ZaxLib.Web.UI.WebControls
{
	public class ZScriptManager : ScriptManager
	{
		public const String SCRIPTLANG = "text/javascript";

		#region Parameters

		[Bindable(false)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Description("Set items in the class to equal heights")]
		[Localizable(true)]
		public String ColumnSizer
		{
			get
			{
				String s = ViewState["ColumnSize"] as String;
				return (s == null ? String.Empty : s);
			}
			set
			{
				ViewState["ColumnSize"] = value;
			}
		}// End parameter ColumnSize

		[Bindable(false)]
		[Category("Library")]
		[DefaultValue("")]
		[Description("Path to the jQuery library scripts")]
		[Localizable(true)]
		public String jQueryPath
		{
			get
			{
				String s = ViewState["jQueryPath"] as String;
				return (s == null ? String.Empty : s);
			}
			set
			{
				ViewState["jQueryPath"] = value;
			}
		}

		[Bindable(false)]
		[Category("Library")]
		[DefaultValue("")]
		[Description("jQuery file name")]
		[Localizable(true)]
		public String jQuery
		{
			get
			{
				String s = ViewState["jQuery"] as String;
				// Check if script is not defined
				if (s == null) s = "jquery-1.10.2.js";
				if (jQueryPath != String.Empty)
				{
					s = String.Format("{0}{1}{2}", jQueryPath, (jQueryPath[jQueryPath.Length - 1] == '/' ? "" : "/"), s);
				}
				return s;
			}
			set
			{
				ViewState["jQuery"] = value;
			}
		}

		[Bindable(false)]
		[Category("Library")]
		[DefaultValue("")]
		[Description("jQueryUI file name")]
		[Localizable(true)]
		public String jQueryUI
		{
			get
			{
				String s = ViewState["jQueryUI"] as String;
				if (s == null) s = "jquery-ui-1.10.4.custom.js";
				if (jQueryPath != String.Empty)
				{
					s = String.Format("{0}{1}{2}", jQueryPath, (jQueryPath[jQueryPath.Length-1] == '/' ? "" : "/"), s);
				}
				return s;
			}
			set
			{
				ViewState["jQueryUI"] = value;
			}
		}

		#endregion

		private ArrayList JSCode;

		public ZScriptManager()
			: base()
		{
			JSCode = new ArrayList();
		}

		public void AddScript(IWriter writer)
		{
			JSCode.Add(writer);
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			// Add script files to page
			Scripts.Add(new ScriptReference(jQuery));
			Scripts.Add(new ScriptReference(jQueryUI));
		}

		protected override void Render(HtmlTextWriter writer)
		{
			// Process base render method
			base.Render(writer);
			if (ColumnSizer.Trim().Length > 0)
			{
				// Break ColumnSizer into an array
				String[] Columns = ColumnSizer.Split(',');
				IWriter CodeWriter = new JQWriter();
				INode top = CodeWriter.CreateTopNode();
				// Create Height Function
				String heightFunc = "EqualHeight";
				String tallVar = "TallestHeight";
				// Name of parameter to pass into the function
				String paramName = "group";

				// Create height function
				INode node = CodeWriter.AddChild("function", top).Function();
				CodeWriter.AddChild(heightFunc).FunctionName();
				CodeWriter.AddParam(paramName, node).VarDef();
				CodeWriter.AddChild(String.Format("var {0} = 0;", tallVar), node).RawCode();
				CodeWriter.AddChild(paramName, node).Method();
				CodeWriter.AddChild("each").Joined();

				INode node2 = CodeWriter.AddParam("function").Function();
				CodeWriter.AddChild("var myHeight = $(this).height();", node2).RawCode();
				CodeWriter.AddChild(String.Format("if(myHeight > {0}) {0} = myHeight;", tallVar)).RawCode();
				CodeWriter.AddChild(paramName, node).Method();
				CodeWriter.AddChild("height").Joined();
				CodeWriter.AddParam(tallVar).VarDef();

				INode nTop = CodeWriter.CreateTopNode();
				CodeWriter.AddChild("document").Selector();
				CodeWriter.AddChild("ready").Joined();
				node = CodeWriter.AddParam("function").Function();
				// Process each item in the columns array
				foreach (String cssClass in Columns)
				{
					CodeWriter.AddChild(heightFunc, node).Method();
					CodeWriter.AddParam(cssClass).SelectorClass();
				}// end foreach cssClass

				CodeWriter.Write(writer);
			}// Endif ColumnSize is defined
		}// End method Render
	}
}
